#include<bits/stdc++.h>
#define int long long
#define N 8005
using namespace std;
int T;
int n,m,a[N];
int x[N],y[N];
int len,p[60005];
int ans;
int b[5];
bool vis[60005],viss[60005];
inline int d(int xx,int yy){
	return abs(x[xx]-x[yy])+abs(y[xx]-y[yy]);
}
inline void sol(){
	scanf("%lld %lld",&n,&m);
	for(int i=1;i<=n;i++) scanf("%lld %lld",&x[i],&y[i]);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	ans=0;
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			for(int k=j+1;k<=n;k++){
				b[1]=d(i,j);b[2]=d(i,k);b[3]=d(j,k);
				sort(b+1,b+3+1);
				if(viss[b[2]]) ans+=a[i]+a[j]+a[k];
			} 
		}
	}
	printf("%lld\n",ans);
}
signed main(){
	freopen("chess.in","r",stdin);
	freopen("chess.out","w",stdout);
	vis[1]=1; 
	for(int i=2;i<=60000;i++){
		if(!vis[i]) p[++len]=i;
		for(int j=1;j<=len&&i*p[j]<=60000;j++){
			vis[i*p[j]]=1;
			if(p[j]%i==0) break;
		} 
	}
	for(int i=1;i<=60000;i++) if(!vis[i]&&(i<=10||i%20==3)) viss[i]=1;
	scanf("%lld",&T);
	while(T--) sol();
	return 0;
}
